package main

/*
 * @lc app=leetcode.cn id=11 lang=golang
 *
 * [11] 盛最多水的容器
 */

// @lc code=start
func maxArea(height []int) int {
	if len(height) < 2 {
		return 0
	}
	if len(height) == 2 {
		return minInt(height[0], height[1]) * 1
	}
	max := 0
	for i, h := range height {
		left, right := 0, 0
		if i > 0 {
			left = getLeftMaxWidth(height[:i], h)
		}
		right = getRightMaxWidth(height[i:], h)
		if max < h*(left+right) {
			max = h * (left + right)
		}
	}
	return max
}

func getLeftMaxWidth(hs []int, h int) int {
	if len(hs) == 0 {
		return 0
	}
	for i, v := range hs {
		if v >= h {
			return len(hs) - i
		}
	}
	return 0
}

func getRightMaxWidth(hs []int, h int) int {
	if len(hs) == 0 {
		return 0
	}
	for i := len(hs) - 1; i >= 0; i-- {
		if hs[i] >= h {
			return i
		}
	}
	return 0
}

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// @lc code=end
